package com.yunshipei.ui.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.yunshipei.enterplorer.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.rong.app.utils.StringUtilSub;

/**
 * Created by cike on 15/12/23.
 */
public class SetNewPasswardActivity extends FindPaasswardBaseActivity {
    private View view = null;
    private EditText newPass1, newPass2;
    private String newPass1Str;
    private String uuid = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        view = layoutInflater.inflate(R.layout.set_new_passward, null);
        super.addContentView(view);
        super.setFindPassStatusColor(3);
        Intent intent = getIntent();
        if (intent != null) {
            uuid = intent.getStringExtra("uuid");
        }
        initView();
    }

    private void initView() {
        newPass1 = (EditText) view.findViewById(R.id.new_pass1);
        newPass2 = (EditText) view.findViewById(R.id.new_pass2);
        super.next.setBtnText("确认提交");
        newPass1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                editTouchListener(v, event);
                return false;
            }
        });
        newPass2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                editTouchListener(v, event);
                return false;
            }
        });
    }

    public boolean editTouchListener(View v, MotionEvent event) {
        Drawable drawable = ((EditText) v).getCompoundDrawables()[2];
        if (drawable != null) {
            if (event.getAction() != MotionEvent.ACTION_UP)
                return false;
            if (event.getX() > newPass1.getWidth() - newPass1.getPaddingRight() - drawable.getIntrinsicWidth()) {
                ((EditText) v).setText("");
            }
        }
        return false;
    }

    @Override
    public void nextClickListener() {
        newPass1Str = newPass1.getText().toString();
        if (StringUtilSub.isPassword(newPass1Str) && newPass1Str.equals(newPass2.getText().toString())) {
            super.showDialog("正在修改，请稍等。。。");
            Map<String, String> map = new HashMap<>();
            map.put("uuid", uuid);
            map.put("password", newPass1Str);
            super.network("/v2/changepwd", map);
        } else if (StringUtilSub.isPassword(newPass1Str)) {
            super.showToast("两次密码不一致");
        } else {
            super.showToast("密码必须是6-16位字母数字组合");
        }
    }

    @Override
    public void onNetworkSuccess(JSONObject jsonObject, String msg) {
        super.hideDialog();
        try {
            String status = jsonObject.getString("status");
            if ("1".equals(status)) {
                String messageString = jsonObject.getString("message");
                if ("密码修改成功".equals(messageString)) {
                    super.showToast(messageString);
                }
                setResult(1);
                finish();
            } else {
                super.showToast("密码修改失败");
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void onNetworkFailure(String errorMsg) {
        super.hideDialog();
        super.showToast(errorMsg);
        Log.e("on failure", errorMsg);
    }
}
